//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.alibaba.nacos.config.server.service.datasource;

import com.alibaba.nacos.common.utils.Preconditions;
import com.alibaba.nacos.common.utils.StringUtils;
import com.zaxxer.hikari.HikariDataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.env.Environment;

public class ExternalDataSourceProperties {
	private static final String JDBC_DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
	private static final String TEST_QUERY = "SELECT 1";
	private Integer num;
	private List<String> url = new ArrayList();
	private List<String> user = new ArrayList();
	private List<String> password = new ArrayList();
	private String jdbcDriverName;

	public String getJdbcDriverName() {
		return jdbcDriverName;
	}

	public void setJdbcDriverName(String jdbcDriverName) {
		this.jdbcDriverName = jdbcDriverName;
	}

	public ExternalDataSourceProperties() {
	}

	public void setNum(Integer num) {
		this.num = num;
	}

	public void setUrl(List<String> url) {
		this.url = url;
	}

	public void setUser(List<String> user) {
		this.user = user;
	}

	public void setPassword(List<String> password) {
		this.password = password;
	}

	List<HikariDataSource> build(Environment environment, ExternalDataSourceProperties.Callback<HikariDataSource> callback) {
		List<HikariDataSource> dataSources = new ArrayList();
		Binder.get(environment).bind("db", Bindable.ofInstance(this));
		Preconditions.checkArgument(Objects.nonNull(this.num), "db.num is null");
		Preconditions.checkArgument(CollectionUtils.isNotEmpty(this.user), "db.user or db.user.[index] is null");
		Preconditions.checkArgument(CollectionUtils.isNotEmpty(this.password), "db.password or db.password.[index] is null");

		for(int index = 0; index < this.num; ++index) {
			int currentSize = index + 1;
			Preconditions.checkArgument(this.url.size() >= currentSize, "db.url.%s is null", new Object[]{index});
			DataSourcePoolProperties poolProperties = DataSourcePoolProperties.build(environment);
			//poolProperties.setDriverClassName("com.mysql.cj.jdbc.Driver");
			if(StringUtils.isNotEmpty(jdbcDriverName)){
				poolProperties.setDriverClassName(jdbcDriverName);
			}else {
				poolProperties.setDriverClassName("com.mysql.cj.jdbc.Driver");
			}
			poolProperties.setJdbcUrl(((String)this.url.get(index)).trim());
			poolProperties.setUsername(((String)com.alibaba.nacos.common.utils.CollectionUtils.getOrDefault(this.user, index, (String)this.user.get(0))).trim());
			poolProperties.setPassword(((String)com.alibaba.nacos.common.utils.CollectionUtils.getOrDefault(this.password, index, (String)this.password.get(0))).trim());
			HikariDataSource ds = poolProperties.getDataSource();
			ds.setConnectionTestQuery("SELECT 1");
			ds.setIdleTimeout(TimeUnit.MINUTES.toMillis(10L));
			ds.setConnectionTimeout(TimeUnit.SECONDS.toMillis(3L));
			dataSources.add(ds);
			callback.accept(ds);
		}

		Preconditions.checkArgument(CollectionUtils.isNotEmpty(dataSources), "no datasource available");
		return dataSources;
	}

	interface Callback<D> {
		void accept(D var1);
	}
}
